/***********************************************************************
 * BinTrotter : BitTorrent library/client for the Android operating system.
 *  
 * Copyright (C) 2009, Christopher David Stones (chris.stones@gmail.com)
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************/

package org.bintrotter;

import java.io.PrintStream;
import java.util.HashMap;

public class Logger {
	
	public static final int LEVEL_NONE 			= 0x0;
	public static final int LEVEL_MSG 			= 0x01;
	public static final int LEVEL_CALL_CLASS 	= 0x02;
	public static final int LEVEL_CALL_METHOD 	= 0x04;
	public static final int LEVEL_CALL_FILE 	= 0x08;
	public static final int LEVEL_CALL_LINE 	= 0x10;
	public static final int LEVEL_STACKTRACE	= 0x20;
	
	public enum MsgType {
		INFO,
		WARN,
		ERR,
		CRIT,
		TRACE,
	}
	
	private HashMap<MsgType, Integer> 	hm;
	private PrintStream 				tw;
	
	public Logger(PrintStream tw) {
		
		this.tw = tw;
		
		/*** default log levels ***/
		hm = new HashMap<MsgType,Integer>();
		
		hm.put(MsgType.INFO, 	LEVEL_MSG );
		hm.put(MsgType.WARN, 	LEVEL_MSG );
		hm.put(MsgType.ERR, 	LEVEL_MSG | LEVEL_CALL_CLASS | LEVEL_CALL_METHOD | LEVEL_CALL_FILE | LEVEL_CALL_LINE );
		hm.put(MsgType.CRIT, 	LEVEL_MSG | LEVEL_CALL_CLASS | LEVEL_CALL_METHOD | LEVEL_CALL_FILE | LEVEL_CALL_LINE | LEVEL_STACKTRACE);
		hm.put(MsgType.TRACE, 	LEVEL_MSG | LEVEL_CALL_CLASS | LEVEL_CALL_METHOD);
	}
	
	public void info(String str) {
		
		write(str,"Info", hm.get(MsgType.INFO));
	}
	
	public void warn(String str) {
		
		write(str,"Warn", hm.get(MsgType.WARN));
	}
	
	public void error(String str) {
		
		write(str,"Error", hm.get(MsgType.ERR));
	}

	public void critical(String str) {
	
		write(str,"Critical", hm.get(MsgType.CRIT));
	}

	public void trace(String str) {
	
		write(str,"Trace", hm.get(MsgType.TRACE));
	}
	
	private void write(String str, String typeStr, int flags) {
		
		final int needStackInfo = 	LEVEL_CALL_CLASS 	|
									LEVEL_CALL_METHOD	|
									LEVEL_CALL_FILE		|
									LEVEL_CALL_LINE		|
									LEVEL_STACKTRACE	;
		
		if((flags & LEVEL_MSG)!=0) {
			
			tw.printf("%s: %s\n",typeStr, str);
		}
		
		if((needStackInfo & flags)!=0) {

			StringBuffer sb = new StringBuffer();
			
			StackTraceElement[] sta = Thread.currentThread().getStackTrace();
			
			if((flags & LEVEL_STACKTRACE)!=0)
			{
				
				for(int i=0; i<sta.length-2; ++i) {
					
					StackTraceElement 	st  = sta[i];
				
					if((flags & LEVEL_CALL_FILE)	!=0) sb.append(st.getFileName() + ": ");
					if((flags & LEVEL_CALL_LINE)	!=0) sb.append(st.getLineNumber() + ": ");
					if((flags & LEVEL_CALL_CLASS)	!=0) sb.append(st.getClassName() + ": ");
					if((flags & LEVEL_CALL_METHOD)	!=0) sb.append(st.getMethodName() + ": ");
					sb.append("\n");
				}
				
			} else {
			
				StackTraceElement 	st  = sta[sta.length - 2];
	
				
				if((flags & LEVEL_CALL_FILE)	!=0) sb.append(st.getFileName() + ": ");
				if((flags & LEVEL_CALL_LINE)	!=0) sb.append(st.getLineNumber() + ": ");
				if((flags & LEVEL_CALL_CLASS)	!=0) sb.append(st.getClassName() + ": ");
				if((flags & LEVEL_CALL_METHOD)	!=0) sb.append(st.getMethodName() + ": ");
			}
			
			tw.println(sb.toString());
		}		
	}
}

